package com.atguigu.guli.service.sms.service.impl;

import com.atguigu.guli.common.util.RandomUtils;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.base.utils.HttpUtils;
import com.atguigu.guli.service.sms.service.SmsService;
import com.atguigu.guli.service.sms.util.SmsProperties;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsProperties smsProperties;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void send(String mobile, String checkCode) throws Exception {
//        Map<String, String> headers = new HashMap<String, String>();
//        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//        headers.put("Authorization", "APPCODE " + smsProperties.getAppCode());
//        Map<String, String> querys = new HashMap<String, String>();
//        querys.put("mobile", mobile);
//        querys.put("param", "code:"+ checkCode);
//        querys.put("tpl_id", smsProperties.getTemplateId1());
//        Map<String, String> bodys = new HashMap<String, String>();
        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";
        String method = "POST";
        String appcode = "f7df12b2584844c4ac5605271ff321f0";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", mobile);
        querys.put("param", "code:"+checkCode);
        querys.put("tpl_id", "TP1711063");
        Map<String, String> bodys = new HashMap<String, String>();


        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//        System.out.println(response.toString());

        System.out.println(response.getStatusLine());
        String data = EntityUtils.toString(response.getEntity(), "UTF-8");
//        System.out.println("data:" +EntityUtils.toString(response.getEntity()));
        Gson gson = new Gson();
        HashMap<String, String> map = gson.fromJson(data, HashMap.class);
        String code = map.get("return_code");

        if ("10001".equals(code)) {
            log.error("手机号码格式错误");
            throw new GuliException(ResultCodeEnum.LOGIN_PHONE_ERROR);
        }

        if (!"00000".equals(code)) {
            log.error("短信发送失败 " + " - code: " + code);
            throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR);
        }
            String key = "checkCode::" + mobile;
            redisTemplate.opsForValue().set(key, checkCode, 5, TimeUnit.MINUTES);

    }
}
